<?php 
namespace frontend\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use frontend\models\User;
use frontend\Components\SaeTOAuthV2;
use frontend\Components\SaeTClientV2;

use frontend\models\Resume;  // 个人简历


class LoginController extends Controller
{
	public $appkey;
	public $appsecret;
	public $callback_url;
	// public $layout='index';
	public function init()
	{
		parent::init();
		session_start();
	}
	// public function actions()
	// {
	//     return [
	//          'captcha' => [
	//               'class' => 'yii\captcha\CaptchaAction',
	//               'maxLength' => 2,
	//               'minLength' => 2
	//          ],
	//      ];
	// }
	// 登录
	public function actionIndex()
	{
		if ($data=Yii::$app->request->post()) {
			

			$name = $data['email'];
			$pwd = $data['password'];

			$sql = 'SELECT * FROM e_user WHERE user_name=:name';
			$res = Yii::$app->db->createCommand($sql,[':name'=>$name])->queryOne();
			if ($res['user_pwd']==$pwd) {
				$_SESSION['user']=$res;
				// 判断 是否为普通用户  或 企业

				if ($res['role'] ) {
                    // 1公司    0个人
					echo '<script>alert("登录成功");location.href="?r=compony/index"</script>';
				}else{


                    // 0 个人  添加简历

                    echo '<script>alert("登录成功");location.href="?r=index/index"</script>';

                }


			}else{
				echo '<script>alert("账号或密码输入错误，请重新登录");location.href="?r=login/index"</script>';
			}



		}else{
			$model=new User();
			return $this->render('index',['model'=>$model]);
		}
	}


	//注册
	public function actionRegister()
	{
		if ($data=Yii::$app->request->post()) {
			$sql = 'SELECT * FROM e_user WHERE user_name=:name';
				$str = Yii::$app->db->createCommand($sql,[':name'=>$data['email']])->queryOne();
			if ($str) {
				echo '<script>alert("注册失败,此账号已经注册过");location.href="?r=login/register"</script>';
			}else{
				$user = new User();
				$name = $users['user_name']=$data['email'];
				$pwd = $users['user_pwd']=$data['password'];
				$role = $users['role']=$data['type'];
				$sql = "INSERT INTO e_user VALUES (NULL,:name,:pwd,:role)";
				$res = Yii::$app->db->createCommand($sql,[':name'=>$name,':pwd'=>$pwd,':role'=>$role])->query();

                $user_id =Yii::$app->db->getLastInsertID(); // 个人用户id

				if ($user_id) {



                    $resume = new Resume();  // 简历表
                    $resume->user_id =$user_id;
                    if( $resume->insert() ){
                        echo '<script>alert("注册成功，请登录");location.href="?r=login/index"</script>';
                    }else{
                        echo '<script>alert("注册失败");location.href="?r=login/register"</script>';
                    }



				}else{
					echo '<script>alert("注册失败");location.href="?r=login/register"</script>';
				}
			}		
		}else{
			$model=new User();
			return $this->renderPartial('register',['model'=>$model]);
		}
	}


	//退出
	public function actionOut()
	{
		unset($_SESSION['user']);
		echo '<script>alert("退出成功");location.href="?r=login/index"</script>';
	}
	public $enableCsrfValidation = false;
	//修改密码
	public function actionShow()
	{
		$this->layout = 'index';
		if ($data=Yii::$app->request->post()) {
				$sql = 'SELECT * FROM e_user WHERE user_name=:name AND user_pwd=:pwd';
				$str = Yii::$app->db->createCommand($sql,[':name'=>$data['user_name'],':pwd'=>$data['oldpassword']])->queryOne();
			if ($str) {
				$sql1 = 'UPDATE e_user set user_pwd=:user_pwd WHERE user_id=:id';
				$user_u = Yii::$app->db->createCommand($sql1,[':id'=>$str['user_id'],':user_pwd'=>$data['newpassword']])->query();
				if ($user_u) {
					unset($_SESSION['user']);
					echo "1";
				}else{
					echo '0';
				}
			}else{
				
				echo '<script>alert("您输入的旧密码不正确");location.href="?r=login/show"</script>';
			}
		}else{
			$user_name=$_SESSION['user']['user_name'];
			return $this->render("show",['user_name'=>$user_name]);
		}
	}
}
 ?>